layui.config({
    base: '/plugins/module/'
});
layui.use(['table','layer','dtree','form','cstmodules'], function () {

    var mymod = layui.cstmodules;
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var dtree = layui.dtree;
    var form = layui.form;

    // 渲染表格
    var sysmgr={
        param:{
            tableColumn:[
                [
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID',align: 'center'},
                    {field: 'parentid', title: '父级ID',align: 'center'},
                    {field: 'menuname', title: '菜单名称',align: 'center'},
                    {field: 'url',  title: '菜单路径',align: 'center'},
                    {field: 'icon',  title: '菜单图标',align:'center',templet:function(d){
                            return "<i class='fa "+d.icon+"'></i>";
                        }},
                    {
                        field: 'type',  align: 'center', templet: function (d) {
                            if (d.type == "h") {
                                return '<span class="layui-badge  typecontnet" >目录</span>';
                            } else {
                                return '<span class="layui-badge-rim typemenu">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    {templet: '#menuRowBar', align: 'center', title: '操作'}
                ]
            ],
            mainIndex:null,
            url:null,
            limit:15,
            limits: [15, 30, 60, 120]

        },
        //删除
        deleteMenu:function(data){
            $.post("/menu/checkMenuHasChildrenNode",{id:data.id},function(result){
                if(result.value){
                    layer.msg("当前菜单节点有子菜单，请选择删除子菜单");
                }else{
                    layer.confirm('你确定要删除【'+data.menuname+'】这条菜单吗?', {icon: 3, title:'提示'}, function(index){
                        $.post("/menu/deleteMenu",{id:data.id},function(res){
                            if(res.code==1){
                                table.reload('menuTable');
                                //刷新下拉树
                                selectTree.reload();
                                layer.msg(res.info);
                            }else {
                                layer.msg("删除失败！")
                            }
                        })
                        layer.close(index);
                    });
                }
            })
        },

        //批量删除
        batchDelete:function  (){
            //得到选中行
            var checkStatus = table.checkStatus('menuTable');
            var dataLength=checkStatus.data.length;
            if(dataLength>0){
                layer.confirm('你确定要删除这些菜单数据吗?', {icon: 3, title:'提示'}, function(index){
                    var data=checkStatus.data; //获取选中行的数据
                    var ids="";
                    $.each(data,function(index,item){
                        if(index==0){
                            ids+="ids="+item.id;
                        }else{
                            ids+="&ids="+item.id;
                        }
                    })
                    $.post("/menu/batchDeletemenu",ids,function(res){
                        if(res.code==1){
                            table.reload('menuTable');
                            layer.msg(res.info);
                        }else {
                            layer.msg("删除失败！")
                        }

                    })
                    layer.close(index);
                });

            }else{
                layer.msg("请选中操作行")
            }
        },

        //打开修改的弹出层
        openUpdateMenuLayer:function (data){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['800px','500px'],
                title:'修改菜单',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    //选中之前父级的菜单  nodeId=data.pid;
                    dtree.dataInit("menuTree", data.parentid);
                    dtree.setSelectValue("menuTree");
                    url="/menu/updateMenu";
                }
            });
        },

        //打开添加的弹出层
        openAddLayer:function(){
            mainIndex=layer.open({
                type:1,
                content:$("#addOrUpdateDiv"),
                area:['900px','500px'],
                title:'添加菜单',
                skin:"layui-layer-lan",
                success:function(){
                    $("#dataFrm")[0].reset();
                    $("#parentid").val("");
                    url="/menu/addMenu";
                }
            });
        },

    }
    mymod.initPageTableMethod(table,"menuTable","/menu/loadAllMenu",sysmgr.param.tableColumn,"#menuToolBar",null,sysmgr.param.limit,sysmgr.param.limits,61);

    //监听工具条
    table.on('tool(menuTable)', function (obj) {
        var data = obj.data;
        switch(obj.event){
            case 'delete':
                sysmgr.deleteMenu(data);
                break;
            case 'update':
                sysmgr.openUpdateMenuLayer(data);
                break;
        };
    });

    //监听头部工具栏
    table.on('toolbar(menuTable)', function(obj){
        switch(obj.event){
            case 'add':
                sysmgr.openAddLayer();
                break;
            case 'batchdelte':
                sysmgr.batchDelete();
                break;
        };
    });

    //模糊查询
    form.on("submit(doSearch)",function(data){
        table.reload('menuTable',{
            where:data.field,
            page:{
                curr:1
            }
        });
        return false;
    });

    //监听提交
    form.on('submit(doSubmit)', function(data){

        $.post(url,data.field,function(res){
            if(res.code==1){
                table.reload('menuTable');
                //刷新下拉树
                selectTree.reload();
                layer.msg(res.info);
            }else {
                layer.msg("操作失败！")
            }

            layer.close(mainIndex);
        })
        return false;
    });

    //初始化下拉树
    var selectTree=dtree.renderSelect({
        elem: "#menuTree",
        width: "100%", // 可以在这里指定树的宽度来填满div
        dataStyle: "layuiStyle",  //使用layui风格的数据格式
        dataFormat: "list",  //配置data的风格为list
        response:{message:"msg",statusCode:0},  //修改response中返回数据的定义
        url: "/menu/loadMenuManagerLeftTreeJson",
    });

    //监听点击的方法
    dtree.on("node(menuTree)" ,function(obj){
        $("#parentid").val(obj.param.nodeId);
    });

});
